﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using Zove.Runtime.Core;

namespace Ascend.Runtime.Core
{
    /// <summary>
    /// The Ascend model, first load, then run and finally run the self test.
    /// </summary>
    public class AscendModel
    {
        private readonly ZoveObject instance;

        public AscendModel()
        {
        }

        public AscendModel(string source)
        {
            Source = source;

            Load = new Dictionary<string, string>();

            instance = new ZoveObject(new AscendLanguageNature());
        }

        public string Source { get; private set; }

        public Dictionary<string, string> Load { get; private set; }

        public List<ZoomEquation> Body { get; private set; }

        public List<string> SelfAssert { get; private set; }

        /// <summary>
        /// Load and run the equatoins.
        /// </summary>
        public void Run()
        {
            foreach (string key in Load.Keys)
            {
                instance.Initialize(key, Load[key]);
            }

            instance.Evaluate();
        }

        public void SelfTest()
        {
            foreach (string assertion in SelfAssert)
            {
                object obj = instance.Evaluate(assertion);
                Debug.Assert(obj != null, "The expression returns null");
                Debug.Assert(Convert.ToBoolean(obj), "The expression is false");
            }
        }

        public override string ToString()
        {
            return instance.Model.ToString();
        }
    }
}